import sys
import os
import random
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        mainLayout = QVBoxLayout()   

        toolBar =QToolBar()
        
        btn = QPushButton('Add Row')
        btn.clicked.connect(self.OnAddRowClicked)
        toolBar.addWidget(btn)

        btn = QPushButton('Add Column')
        btn.clicked.connect(self.OnAddColumnClicked)
        toolBar.addWidget(btn)        

        btn = QPushButton('Delete')
        btn.clicked.connect(self.OnDelClicked)
        toolBar.addWidget(btn)

        toolBar.addWidget(QLabel('Update:'))
        self.currentEdit = QLineEdit()
        toolBar.addWidget(self.currentEdit)
        btn = QPushButton('Update')
        btn.clicked.connect(self.OnUpdateClicked)
        toolBar.addWidget(btn)    

        toolBar.addWidget(QLabel('Keyword:'))
        self.keyEdit = QLineEdit()
        toolBar.addWidget(self.keyEdit)
        btn = QPushButton('Search')
        btn.clicked.connect(self.OnSearchClicked)
        toolBar.addWidget(btn)
        
        self.tableView = QTableView()
        self.tableData = QStandardItemModel()
        self.tableView.setModel(self.tableData)
        self.tableData.setHorizontalHeaderLabels(['C1','C2','C3'])
        mainLayout.addWidget(toolBar)
        mainLayout.addWidget(self.tableView)
        self.setLayout(mainLayout)     
    def OnAddRowClicked(self):        
        self.tableData.appendRow([QStandardItem(''),QStandardItem(''),QStandardItem('')])
        self.parent.OnShowMessage(f'Add row,count {self.tableData.rowCount()}')
    def OnAddColumnClicked(self):        
        columnCount=self.tableData.columnCount()
        self.tableData.setColumnCount(columnCount+1)
        columnName=[]
        for i in range(1,self.tableData.columnCount()+1):
            columnName.append(f'C{i}')
        self.tableData.setHorizontalHeaderLabels(columnName)        
        self.parent.OnShowMessage(f'Add column,count {self.tableData.columnCount()}')
    def OnDelClicked(self):
        currentIndex = self.tableView.currentIndex()
        currentItem = self.tableData.itemFromIndex(currentIndex)
        if currentItem is not None:            
            self.parent.OnShowMessage(f"Delete item '{currentItem.text()}'")            
            currentItem.setText(None)
        
    def OnUpdateClicked(self):
        currentIndex = self.tableView.currentIndex()
        currentItem = self.tableData.itemFromIndex(currentIndex)
        old = currentItem.text()
        new = self.currentEdit.text()
        currentItem.setText(new)
        self.parent.OnShowMessage(f"Update item from '{old}' to '{new}'")
    def OnSearchClicked(self):
        keyword = self.keyEdit.text()
        msg = f"Searh keyword '{keyword}' result '"
        for item in self.tableData.findItems(keyword,Qt.MatchFlag.MatchContains):
            msg += f'{item.text()},'
        msg+="'"
        self.parent.OnShowMessage(msg)
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet